define(['egis'], function (egis) {
    Window.deserializer = Window.deserializer || new egis.Deserializer();
    Window.container = Window.container || {};

    dsBridge.register('create_FeatureLayer', function (sourceId, layerId, name, zIndex) {
        var layer = new egis.carto.FeatureLayer({
            "id": layerId,
            "name": name,
            "zIndex": zIndex
        });
        Window.container[sourceId] = layer;
    })

    dsBridge.register('FeatureLayer_setSource', function (sourceId, source) {
        var layer = Window.container[sourceId];
        source = JSON.parse(source);
        layer.setSource(source);
    })

    dsBridge.register('FeatureLayer_add', function (sourceId, featureId, content) {
        try {
            var featureLayer = Window.container[sourceId];
            var target = Window.container[featureId];
            var feature = Window.deserializer.createJsInstance(JSON.parse(content));
            if (!target) {
                target = feature;
            }
            else {
                target.fields = feature.fields;
                target.properties = feature.properties;
            }
            featureLayer.addFeature(target);
        }
        catch (e) {
            alert(e)
        }
    })

    dsBridge.register('FeatureLayer_addFeatureCollection', function (sourceId, featureCollection) {
        try {
            var featureLayer = Window.container[sourceId];
            featureCollection = Window.deserializer.createJsInstance(JSON.parse(featureCollection));
            featureCollection.reset();
            featureLayer.addFeatureCollection(featureCollection)
        }
        catch (e) {
            alert(e)
        }
    })

    dsBridge.register('FeatureLayer_render', function (sourceId, featureRenderId) {
        try {
            var source = Window.container[sourceId];
            var target = Window.container[featureRenderId];
            source.render(target);
        } catch (e) {
            alert(e)
        }
    })

    dsBridge.register('FeatureLayer_addFeatures', function (sourceId, feature) {
        var featureLayer = Window.container[sourceId];
        feature = Window.deserializer.createJsInstance(JSON.parse(feature));
        featureLayer.add(feature);
    })

    dsBridge.register('FeatureLayer_remove', function (sourceId, featureId) {
        var source = Window.container[sourceId];
        var target = Window.container[featureId]
        source.remove(target);
    })

    dsBridge.register('FeatureLayer_updateEntity', function (sourceId, featureId, content) {
        var source = Window.container[sourceId];
        var target = Window.container[featureId];
        var feature = Window.deserializer.createJsInstance(JSON.parse(content));
        target.fields = feature.fields;
        target.properties = feature.properties;
        source.updateEntity(target);
    })

    dsBridge.register('FeatureLayer_hitTest', function (sourceId, screenX, screenY) {
        try {
            var source = Window.container[sourceId];
            var result = source.hitTest(screenX, screenY);
            if(!!result){
                var gFeature = result['feature'];
                var olFeature =gFeature.olFeature;
                delete gFeature['olFeature'];
                var feature = JSON.stringify(gFeature)
                gFeature['olFeature'] = olFeature;
                return feature;
            }
            return null;
        }
        catch (e) {
            alert(e);
        }
    })

    dsBridge.register('FeatureLayer_hitTestList', function (sourceId, screenX, screenY) {
        var source = Window.container[sourceId];
        return source.hitTestList(screenX, screenY);
    })

    dsBridge.register('FeatureLayer_select', function (sourceId, featureId) {
        var source = Window.container[sourceId];
        var feature = Window.container[featureId];
        var graphic = new egis.carto.Graphic({
            layer: source,
            feature: feature
        });
        source.select(graphic);
    })

    dsBridge.register('FeatureLayer_unSelect', function (sourceId, featureId) {
        var source = Window.container[sourceId];
        var feature = Window.container[featureId];
        var graphic = new egis.carto.Graphic({
            layer: source,
            feature: feature
        });
        source.select(graphic);
    })

    dsBridge.register('FeatureLayer_unRender', function (sourceId, featureRenderId) {
        var source = Window.container[sourceId];
        var target = Window.container[featureRenderId];
        source.unRender(target);
    })

    dsBridge.register('FeatureLayer_selectEdit', function (sourceId, featureId) {
        var source = Window.container[sourceId];
        var feature = Window.container[featureId];
        var graphic = new egis.carto.Graphic({
            layer: source,
            feature: feature
        });
        source.select(graphic);
    })

    dsBridge.register('FeatureLayer_unSelectEdit', function (sourceId, featureId) {
        var source = Window.container[sourceId];
        var feature = Window.container[featureId];
        var graphic = new egis.carto.Graphic({
            layer: source,
            feature: feature
        });
        source.select(graphic);
    })

    dsBridge.register('FeatureLayer_getExtent', function (sourceId) {
        var source = Window.container[sourceId];
        return source.getExtent();

    })
})